using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ClimbTracker.BusinessLogic.Entities;
using MySql.Data.MySqlClient;
using System.IO;

namespace ClimbTracker.Reports
{
    public partial class ClimbingAttemptReportForm : Form
    {
        public ClimbingAttemptReportForm()
        {
            InitializeComponent();
            ClimbTrackerDataSet ds = new ClimbTrackerDataSet();
            
            MySqlConnection myConnection = new MySqlConnection("server=localhost;user id=root;database=climbtracker");
            MySqlDataAdapter competitorAdapter = new MySqlDataAdapter(myConnection.CreateCommand());
            //competitorAdapter.SelectCommand.CommandText = "select * from Competitors WHERE Competitors.Name = '" + ClimbTracker.GUI.TabControls.CompetitorsTabControl.SelectedCompetitor.Name + "',select * from Routes where Routes.Name = ' " + ClimbTracker.GUI.TabControls.RoutesTabControl.SelectedRoute.Name + "'";
            competitorAdapter.SelectCommand.CommandText = "select * from Routes where Routes.Name = ' " + ClimbTracker.GUI.TabControls.RoutesTabControl.SelectedRoute.Name + "'";
            competitorAdapter.Fill(ds.competitors);
            
            //MySqlDataAdapter routesAdapter = new MySqlDataAdapter(myConnection.CreateCommand());
            //routesAdapter.SelectCommand.CommandText = "select * from Routes WHERE RouteID = ' " + ClimbTracker.GUI.TabControls.RoutesTabControl.SelectedRoute.RouteID + "'";
            //routesAdapter.Fill(ds.routes);

            ClimbTracker.Reports.ClimbAttemptReport rep = new ClimbTracker.Reports.ClimbAttemptReport();


            // Enables presenting images in the report
            for (int index = 0; index < ds.competitors.Rows.Count; index++)
            {
                if (ds.competitors.Rows[index]["PicturePath"].ToString() != "")
                {
                    string s = ds.competitors.Rows[index]["PicturePath"].ToString();

                    if (File.Exists(s))
                    {
                        LoadImage(ds.competitors.Rows[index], "imagePath", s);
                    }
                    else
                    {
                        LoadImage(ds.competitors.Rows[index], "imagePath",
                                  "DefaultPicturePath");
                    }
                }
                else
                {
                    LoadImage(ds.competitors.Rows[index], "imagePath",
                              "DefaultPicturePath");
                }
            }

            for (int index = 0; index < ds.routes.Rows.Count; index++)
            {
                if (ds.routes.Rows[index]["PicturePath"].ToString() != "")
                {
                    string s = ds.routes.Rows[index]["PicturePath"].ToString();

                    if (File.Exists(s))
                    {
                        LoadImage(ds.routes.Rows[index], "imagePath", s);
                    }
                    else
                    {
                        LoadImage(ds.routes.Rows[index], "imagePath",
                                  "DefaultPicturePath");
                    }
                }
                else
                {
                    LoadImage(ds.routes.Rows[index], "imagePath",
                              "DefaultPicturePath");
                }
            }

            try
            {
                // Finally display report in crystal report viewer
                rep.Load("ClimbingAttemptReport.rpt");
                rep.SetDataSource(ds);
                crystalReportViewer1.ReportSource = rep;
            }
            catch (Exception ex)
            {
                throw;
            }

            rep.SetDataSource(ds);
            crystalReportViewer1.ReportSource = rep;

        }



        private void LoadImage(DataRow objDataRow, string strImageField, string FilePath)
        {
            try
            {
                FileStream fs = new FileStream(FilePath,
                           System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] Image = new byte[fs.Length];
                fs.Read(Image, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                objDataRow[strImageField] = Image;
            }
            catch (Exception ex)
            {
                //Response.Write("<font color=red>" + ex.Message + "</font>");
            }
        }
    }
}